package com.czy.owner.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.czy.owner.R;
import com.czy.owner.entity.CarBrandModel;
import com.czy.owner.entity.PackageSelectModel;
import com.czy.owner.entity.SelectBespokeListModel;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.utils.StringUtils;
import com.czy.owner.utils.TimeUtils;
import com.czy.owner.widget.RoundImageView;
import com.czy.owner.widget.shopbutton.AnimShopButton;
import com.czy.owner.widget.shopbutton.IOnAddDelListener;

import org.xutils.common.Callback;
import org.xutils.x;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Created by hedepu on 2017-08-24
 * unlimitedCount 是否有次数限制 hep 2018/01/31
 */

public class SelectCardAdapter extends BaseRecyclerAdapter<PackageSelectModel, SelectCardAdapter.SelectCardHolder> implements RecyclerHeadersAdapter<RecyclerView.ViewHolder> {
    Toast toast;
    LinkedHashMap<String, PackageSelectModel> packageMap = new LinkedHashMap<String, PackageSelectModel>();
    private List<PackageSelectModel> selectedList = new ArrayList<>();
    private int MAXCOUNT = 999;

    public SelectCardAdapter(Context context, List<PackageSelectModel> data) {
        super(context, data);

    }


    public void setSelectedList(List<PackageSelectModel> list) {
        packageMap.clear();
        for (PackageSelectModel packageSelectModel : list) {
            packageMap.put(packageSelectModel.getUserOrderId() + "-" + packageSelectModel.getPackageItemListBean().getUserPackageItemId(), packageSelectModel);
        }
    }

    public List<PackageSelectModel> getSelectedList() {
        selectedList.clear();
        for (Map.Entry<String, PackageSelectModel> entry : packageMap.entrySet()) {
            selectedList.add(entry.getValue());
        }
        return selectedList;
    }

    @Override
    public SelectCardAdapter.SelectCardHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.item_select_card, parent, false);
        return new SelectCardAdapter.SelectCardHolder(view);
    }

    @Override
    public void onBindViewHolder(SelectCardAdapter.SelectCardHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        holder.bindData(data.get(position), position);
    }


    @Override
    public long getHeaderId(int position) {
        return data.get(position).getDes();
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        View view = layoutInflater.inflate(R.layout.recycler_package_header, parent, false);
        return new RecyclerView.ViewHolder(view) {
        };
    }

    @Override
    public int getItemCount() {
        return data.size();
    }


    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {
        TextView textView = (TextView) holder.itemView.findViewById(R.id.tv_recycler_header);
        TextView tv_recycler_header_time = (TextView) holder.itemView.findViewById(R.id.tv_recycler_header_time);
        if ("0".equals(data.get(position).getEndTime())) {
            tv_recycler_header_time.setText("有效期：永久有效");
        } else {
            tv_recycler_header_time.setText("有效期：" + TimeUtils.timeStamp2Date(data.get(position).getStartTime(), "yyyy.MM.dd") + "-" +
                    TimeUtils.timeStamp2Date(data.get(position).getEndTime(), "yyyy.MM.dd"));
        }
        textView.setText(data.get(position).getPackageName());
    }

    public class SelectCardHolder extends BaseRecyclerAdapter.ViewHolder {
        private TextView tv_card_name, tv_card_max_num, tv_project_type;
        private AnimShopButton btn_anmi_shop;

        public SelectCardHolder(View itemView) {
            super(itemView);
            tv_card_name = (TextView) itemView.findViewById(R.id.tv_card_name);
            tv_card_max_num = (TextView) itemView.findViewById(R.id.tv_card_max_num);
            tv_project_type = (TextView) itemView.findViewById(R.id.tv_project_type);
            btn_anmi_shop = (AnimShopButton) itemView.findViewById(R.id.btn_anmi_shop);
//            setIsRecyclable(false);
        }

        public void bindData(final PackageSelectModel mMembers, int position) {

            if ("0".equals(StringUtils.getString(mMembers.getPackageItemListBean().getCount(), "0")) && mMembers.getPackageItemListBean().isUnlimitedCount()) {
                btn_anmi_shop.setVisibility(View.GONE);
            } else {
                btn_anmi_shop.setVisibility(View.VISIBLE);

            }

            tv_card_name.setText(mMembers.getPackageItemListBean().getItemName());
            if (!mMembers.getPackageItemListBean().isUnlimitedCount()) {
                tv_card_max_num.setText("(不限次)");
                btn_anmi_shop.setMaxCount(MAXCOUNT);
            } else {
                if (Integer.parseInt(StringUtils.getString(mMembers.getPackageItemListBean().getCount(), "0")) >= MAXCOUNT) {
                    btn_anmi_shop.setMaxCount(MAXCOUNT);
                } else {
                    btn_anmi_shop.setMaxCount(Integer.parseInt(StringUtils.getString(mMembers.getPackageItemListBean().getCount(), "0")));

                }
                tv_card_max_num.setText("(剩余" + StringUtils.getString(mMembers.getPackageItemListBean().getCount(), "0") + ")");
            }

            if (packageMap.containsKey(mMembers.getUserOrderId() + "-" + mMembers.getPackageItemListBean().getUserPackageItemId())) {
                mMembers.getPackageItemListBean().setCurrentCount(packageMap.get(mMembers.getUserOrderId() + "-" + mMembers.getPackageItemListBean().getUserPackageItemId()).getPackageItemListBean().getCurrentCount());
                btn_anmi_shop.setCount(mMembers.getPackageItemListBean().getCurrentCount());
//                tv_card_name.setText(mMembers.getUserOrderId() + "-" + mMembers.getPackageItemListBean().getItemId() + "-" + position);
//                Toast.makeText(mContext, , Toast.LENGTH_SHORT).show();
            } else {
                btn_anmi_shop.setCount(0);

            }


            if ("goods".equals(mMembers.getPackageItemListBean().getItemType())) {
                if (tv_project_type.getVisibility() == View.GONE)
                    tv_project_type.setVisibility(View.VISIBLE);
                tv_project_type.setText(mMembers.getPackageItemListBean().getSpecName());
            } else {
                if (tv_project_type.getVisibility() == View.VISIBLE)
                    tv_project_type.setVisibility(View.GONE);
            }
            btn_anmi_shop.setOnAddDelListener(new IOnAddDelListener() {
                @Override
                public void onAddSuccess(int count) {
                    mMembers.getPackageItemListBean().setCurrentCount(count);
                    packageMap.put(mMembers.getUserOrderId() + "-" + mMembers.getPackageItemListBean().getUserPackageItemId(), mMembers);

                }

                @Override
                public void onAddFailed(int count, FailType failType) {
                    if (count == MAXCOUNT) {
                        showToastShort("最多不能超出" + MAXCOUNT + "次");
                    } else if (failType == FailType.COUNT_MAX) {
                        showToastShort("最多不能超出剩余次数");
                    }
                }

                @Override
                public void onDelSuccess(int count) {
                    mMembers.getPackageItemListBean().setCurrentCount(count);

                    if (count == 0) {
                        packageMap.remove(mMembers.getUserOrderId() + "-" + mMembers.getPackageItemListBean().getUserPackageItemId());
                    } else {
                        packageMap.put(mMembers.getUserOrderId() + "-" + mMembers.getPackageItemListBean().getUserPackageItemId(), mMembers);
                    }


                }

                @Override
                public void onDelFaild(int count, FailType failType) {

                }
            });
//            animShopButton.setReplenish(addDelBean.isReplenish());

        }
    }

    public void showToastShort(String message) {
        if (toast == null) {
            toast = Toast.makeText(mContext, message, Toast.LENGTH_SHORT);
        } else {
            toast.setText(message);
            toast.setDuration(Toast.LENGTH_SHORT);
        }
        toast.show();
    }
}

